package com.SMS;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSRecv extends BroadcastReceiver {
	private static final String queryString = "@echo";
	private static final String SMS_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public void onReceive(Context arg0, Intent arg1) {
		if (arg1.getAction().equals(SMS_RECEIVER)) {
			SmsManager sms = SmsManager.getDefault();
			Bundle bundle = arg1.getExtras();
			if (bundle != null) {
				Object[] pdus = (Object[]) bundle.get("pdus");
				SmsMessage[] messages = new SmsMessage[pdus.length];
				for (int i = 0; i < pdus.length; i++)
					messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				for (SmsMessage message : messages) {
					String msg = message.getMessageBody();
					String to = message.getOriginatingAddress();
					if (msg.toLowerCase().startsWith(queryString)) {
						String out = msg.substring(queryString.length());
						sms.sendTextMessage(to, null, out, null, null);
						Toast.makeText(arg0, "success", Toast.LENGTH_LONG)
								.show();
					}
				}
			}
		}

	}
}
